package com.squid.dataCenter.service.datadictionaryparent;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.squid.base.common.enums.CommonErrorEnums;
import com.squid.base.common.enums.DeleteTokenEnums;
import com.squid.base.common.error.ExceptionFactory;
import com.squid.base.common.mapper.EasyServiceImpl;
import com.squid.dataCenter.convertor.DataDictionaryParentEntityConvertor;
import com.squid.dataCenter.model.entity.DataDictionaryParentEntity;
import com.squid.dataCenter.model.mapper.DataDictionaryParentEntityMapper;
import com.squid.dataCenter.model.req.datadictionaryparent.DataDictionaryParentInsertReq;
import com.squid.dataCenter.model.req.datadictionaryparent.DataDictionaryParentUpdateReq;
import com.squid.dataCenter.model.resp.datadictionaryparent.DataDictionaryParentResp;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 数据字典服务
 *
 * @author jinhang1
 * @date 2024/06/27
 */
@Service
public class DataDictionaryParentServiceImpl extends EasyServiceImpl<DataDictionaryParentEntityMapper, DataDictionaryParentEntity>
        implements DataDictionaryParentService {
    /**
     * 选择数据dic父列表
     *
     * @return {@link List }<{@link DataDictionaryParentResp }>
     */
    @Override
    public List<DataDictionaryParentResp> selectDataDicParentList() {
        List<DataDictionaryParentResp> response = null;
        try {
            List<DataDictionaryParentEntity> list = this.list(new LambdaQueryWrapper<DataDictionaryParentEntity>().orderByAsc(DataDictionaryParentEntity::getUpdateTime));
            response = DataDictionaryParentEntityConvertor.INSTANCE.from(list);
        } catch (Exception e) {
            throw ExceptionFactory.instanceBusinessException(CommonErrorEnums.CODE_1003, e);
        }
        return response;
    }

    private void repeat(String code, Long id) {
        DataDictionaryParentEntity dataDictionaryParentEntity = this.getOne(new LambdaQueryWrapper<DataDictionaryParentEntity>()
                .eq(DataDictionaryParentEntity::getCode, code)
                .eq(DataDictionaryParentEntity::getDeleteToken, DeleteTokenEnums.N.getCode())
                .ne(!ObjectUtils.isEmpty(id), DataDictionaryParentEntity::getId, id)
        );
        if (!ObjectUtils.isEmpty(dataDictionaryParentEntity)) {
            throw ExceptionFactory.instanceBusinessException(CommonErrorEnums.CODE_1001);
        }
    }

    @Override
    public void add(DataDictionaryParentInsertReq req) {
        DataDictionaryParentEntity dataDictionaryParentEntity = DataDictionaryParentEntityConvertor.INSTANCE.insertTo(req);
        this.repeat(req.getCode(), null);
        try {
            this.save(dataDictionaryParentEntity);
        } catch (Exception e) {
            throw ExceptionFactory.instanceServerException(CommonErrorEnums.CODE_1005, e);
        }
    }

    @Override
    public void update(DataDictionaryParentUpdateReq req) {
        DataDictionaryParentEntity dataDictionaryParentEntity = DataDictionaryParentEntityConvertor.INSTANCE.updateTo(req);
        this.repeat(req.getCode(), req.getId());
        try {
            this.updateById(dataDictionaryParentEntity);
        } catch (Exception e) {
            throw ExceptionFactory.instanceServerException(CommonErrorEnums.CODE_1002, e);
        }
    }

    @Override
    public void delete(Long id) {
        try {
            this.removeById(id);
        } catch (Exception e) {
            throw ExceptionFactory.instanceServerException(CommonErrorEnums.CODE_1004, e);
        }
    }
}
